$(function(){

	$("#recipearticle-add-edit select.foodsectionid").change(function(){
		var sid = $("#recipearticle-add-edit select.foodsectionid :selected").val();
		$.post(APP+'/FoodArticle/returns', {id: sid}, function(data){
			var obj = JSON.parse(data);
			var foodcategory = $("#recipearticle-add-edit select.foodcategoryid");
			foodcategory.html("");
			$('<option value="0">---请选择所属分类---</option>').appendTo(foodcategory);
			for(var i=0; i<obj.length; i++){
				$('<option value="'+obj[i].id+'">'+obj[i].name+'</option>').appendTo(foodcategory);
			}
		});
	});

	$("#recipearticle-add-edit table textarea").each(function(index){
		var li = $(this);
		li.click(function(){
				$("#recipearticle-add-edit table textarea").removeClass("textarea");
				li.addClass("textarea");
		});

	});

	$("#recipearticle-add-edit input.submit").click(function(){
		var sid = $("#recipearticle-add-edit select.foodsectionid :selected").val();
		var cid = $("#recipearticle-add-edit select.foodcategoryid :selected").val();
		if(sid==0){
			alert('请选择所属栏目');
			return false;
		}else if(cid==0){
			alert('请选择所属分类');
			return false;
		}
	});

	$("#recipearticle-index .allselect").toggle(function(){
		$("#recipearticle-index input[type=checkbox]").attr('checked', 'checked');
	},function(){
		$("#recipearticle-index input[type=checkbox]").attr('checked',null);
	});

	$("#recipearticle-index .buttondel").click(function(){
		var datas = $("#recipearticle-index form.delete").serializeArray();
		if(datas==''){
			alert('请选择要删除的菜谱');
		}
		else{
			if(window.confirm('确认删除这些菜谱?')){
				$("#recipearticle-index form.delete").submit();
			}
		}
	});

	$("#recipearticle-index  .nav a").each(function(index){
		var a = $(this);
		if(a.attr('href')==SELF){
			a.css('background-color','yellow');
			return false;
		}
		if(eval("/"+sids+"/.test(a.attr('href'))")){
			a.css('background-color','yellow');
			return false;
		}
	});

	$("#recipearticle-index  .cnav a").each(function(index){
		var a = $(this);
		if(a.attr('href')==SELF){
			a.css('background-color','yellow');
			return false;
		}
	});

	$("#recipearticle-index form.search").submit(function(){
		var key = $("#recipearticle-index input[name=key]").val();
		if(key==''){
			alert('搜索菜谱不能为空');
			return false;
		}
	});

	$("#recipearticle-index tbody tr").hover(function(){
		$(this).css("background-color", "#d9e6ff");
//		$(this).find(".display").css("display", "block");
	}, function(){
		$(this).css("background-color", "white");
//		$(this).find(".display").css("display", "none");
	});

	var text = $("#recipearticle-add-edit .pic");
	var button = $("#recipearticle-add-edit form .upload");
	var fileType = "pic",fileNum = "one";

	new AjaxUpload(button,{
		action: APP+'/FoodArticle/uploads',
		name: 'pic',
		onSubmit : function(file, ext){
			if(fileType == "image")
			{
				if (ext && /^(jpg|png|jpeg|gif)$/.test(ext)){
					this.setData({
						'info': '文件类型为图片'
					});
				} else {
					alert('非图片类型文件，请重传');
					return false;
				}
			}
		},
		onComplete: function(file, response){
			if(isNaN(response[0])){
				alert(response);

			}else{
				text.val(response);
				$("#recipearticle-add-edit .img img").attr('src', PUBLIC+"/Content/tmp/"+response);
			}
		}
	});

});
